package com.dddpeter.app.controller;

import com.dddpeter.app.entity.Comment;
import com.dddpeter.app.service.CommentService;
import com.dddpeter.app.service.ArticleLikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 评论控制器
 * 评论功能允许匿名访问，不需要登录验证
 */
@RestController
@RequestMapping("/api/comments")
@CrossOrigin(origins = "*")
public class CommentController {
    
    @Autowired
    private CommentService commentService;
    
    @Autowired
    private ArticleLikeService articleLikeService;
    
    /**
     * 获取文章评论列表
     */
    @GetMapping("/article/{articleId}")
    public ResponseEntity<Map<String, Object>> getCommentsByArticleId(@PathVariable Long articleId) {
        try {
            List<Comment> comments = commentService.getCommentsByArticleId(articleId);
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("data", comments);
            response.put("message", "获取评论列表成功");
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", "获取评论列表失败: " + e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }
    
    /**
     * 添加评论
     */
    @PostMapping
    public ResponseEntity<Map<String, Object>> addComment(@RequestBody Comment comment) {
        try {
            Comment savedComment = commentService.addComment(comment);
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("data", savedComment);
            response.put("message", "评论添加成功");
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", "评论添加失败: " + e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }
    
    /**
     * 点赞评论
     */
    @PostMapping("/{commentId}/like")
    public ResponseEntity<Map<String, Object>> likeComment(
            @PathVariable Long commentId, 
            @RequestParam String nickname) {
        try {
            boolean success = commentService.likeComment(commentId, nickname);
            
            Map<String, Object> response = new HashMap<>();
            if (success) {
                response.put("success", true);
                response.put("message", "点赞成功");
            } else {
                response.put("success", false);
                response.put("message", "您已经点赞过了");
            }
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", "点赞失败: " + e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }
    
    /**
     * 点赞文章
     */
    @PostMapping("/article/{articleId}/like")
    public ResponseEntity<Map<String, Object>> likeArticle(
            @PathVariable Long articleId, 
            @RequestParam String nickname) {
        try {
            boolean success = articleLikeService.likeArticle(articleId, nickname);
            
            Map<String, Object> response = new HashMap<>();
            if (success) {
                response.put("success", true);
                response.put("message", "点赞成功");
            } else {
                response.put("success", false);
                response.put("message", "您已经点赞过了");
            }
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", "点赞失败: " + e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }
    
    /**
     * 检查用户是否已点赞评论
     */
    @GetMapping("/{commentId}/like-status")
    public ResponseEntity<Map<String, Object>> checkCommentLikeStatus(
            @PathVariable Long commentId, 
            @RequestParam String nickname) {
        try {
            boolean hasLiked = commentService.hasLikedComment(commentId, nickname);
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("hasLiked", hasLiked);
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", "检查点赞状态失败: " + e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }
    
    /**
     * 检查用户是否已点赞文章
     */
    @GetMapping("/article/{articleId}/like-status")
    public ResponseEntity<Map<String, Object>> checkArticleLikeStatus(
            @PathVariable Long articleId, 
            @RequestParam String nickname) {
        try {
            boolean hasLiked = articleLikeService.hasLikedArticle(articleId, nickname);
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("hasLiked", hasLiked);
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", "检查点赞状态失败: " + e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }
}
